package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.fundset.pojo.dto.FundSetConditionAttrDTO;
import com.xbongbong.pro.fundset.pojo.dto.FundSetDTO;
import com.xbongbong.pro.fundset.pojo.dto.FundSetListDTO;
import com.xbongbong.pro.fundset.pojo.dto.FundSetPayDTO;
import com.xbongbong.pro.fundset.pojo.dto.FundSetPaySaveDTO;
import com.xbongbong.pro.fundset.pojo.dto.FundSetPaymentSaveDTO;
import com.xbongbong.pro.fundset.pojo.vo.FundSetConditionAttrVO;
import com.xbongbong.pro.fundset.pojo.vo.FundSetListVO;
import com.xbongbong.pro.fundset.pojo.vo.FundSetPayListVO;
import com.xbongbong.pro.fundset.pojo.vo.FundSetPayVO;
import com.xbongbong.pro.fundset.pojo.vo.FundSetPaymentItemVO;
import com.xbongbong.pro.fundset.pojo.vo.FundSetPaymentSaveVO;
import com.xbongbong.pro.fundset.pojo.vo.FundSetVO;
import com.xbongbong.saas.service.FundSetService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zcp
 * @version v1.0
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/fund/set")
public class FundSetController {
    @Resource
    private FundSetService fundSetService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 资金设置菜单列表
     * @param fundSetListDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid FundSetListDTO fundSetListDTO, BindingResult br) {
        XbbResponse<FundSetListVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetListVO fundSetListVO = fundSetService.getMenuList(fundSetListDTO);
                response = new XbbResponse<>(fundSetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 应收规则详情、编辑获取
     * @param baseDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @PostMapping(value = "/payment/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String paymentItem(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<FundSetPaymentItemVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetPaymentItemVO fundSetPaymentItemVO = fundSetService.paymentItem(baseDTO);
                response = new XbbResponse<>(fundSetPaymentItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 设置账期条件字段
     * @param fundSetConditionAttrDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @PostMapping(value = "/conditionAttr", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String conditionAttr(@RequestBody @Valid FundSetConditionAttrDTO fundSetConditionAttrDTO, BindingResult br) {
        XbbResponse<FundSetConditionAttrVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetConditionAttrVO fundSetConditionAttrVO = fundSetService.conditionAttr(fundSetConditionAttrDTO);
                response = new XbbResponse<>(fundSetConditionAttrVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 应收规则保存
     * @param fundSetPaymentSaveDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @PostMapping(value = "/payment/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String paymentSave(@RequestBody @Valid FundSetPaymentSaveDTO fundSetPaymentSaveDTO, BindingResult br) {
        XbbResponse<FundSetPaymentSaveVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetPaymentSaveVO fundSetPaymentSaveVO = fundSetService.paymentSave(fundSetPaymentSaveDTO);
                response = new XbbResponse<>(fundSetPaymentSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 信用额度设置-展示
     * @param baseDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/credit/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String creditItem(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<FundSetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetVO fundSetVO = fundSetService.creditItem(baseDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 信用额度设置-开启、关闭
     * @param fundSetDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/credit/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String creditSave(@RequestBody @Valid FundSetDTO fundSetDTO, BindingResult br) {
        XbbResponse<FundSetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetVO fundSetVO = fundSetService.creditSave(fundSetDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 对账设置-展示
     * @param baseDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/statement/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String statementItem(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<FundSetPaymentItemVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetPaymentItemVO fundSetPaymentItemVO = fundSetService.statementItem(baseDTO);
                response = new XbbResponse<>(fundSetPaymentItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 对账设置-保存
     * @param fundSetPaymentSaveDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/statement/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String statementSave(@RequestBody @Valid FundSetPaymentSaveDTO fundSetPaymentSaveDTO, BindingResult br) {
        XbbResponse<FundSetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetVO fundSetVO = fundSetService.statementSave(fundSetPaymentSaveDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 核销设置-展示
     * @param baseDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/writeOff/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String writeOffItem(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<FundSetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetVO fundSetVO = fundSetService.writeOffItem(baseDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 核销设置-保存
     * @param fundSetDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/writeOff/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String writeOffSave(@RequestBody @Valid FundSetDTO fundSetDTO, BindingResult br) {
        XbbResponse<FundSetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetVO fundSetVO = fundSetService.writeOffSave(fundSetDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 支付设置列表-展示
     * @param baseDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/pay/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String payList(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<FundSetPayListVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetPayListVO fundSetPayListVO = fundSetService.payList(baseDTO);
                response = new XbbResponse<>(fundSetPayListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 支付设置展示
     * @param fundSetPayDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/pay/item", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String payItem(@RequestBody @Valid FundSetPayDTO fundSetPayDTO, BindingResult br) {
        XbbResponse<FundSetPayVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetPayVO fundSetVO = fundSetService.payItem(fundSetPayDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 支付设置-保存
     * @param fundSetPaySaveDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/pay/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String paySave(@RequestBody @Valid FundSetPaySaveDTO fundSetPaySaveDTO, BindingResult br) {
        XbbResponse<FundSetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetVO fundSetVO = fundSetService.paySave(fundSetPaySaveDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 支付设置-开启，关闭
     * @param fundSetDTO
     * @param br
     * @return
     */
    @PostMapping(value = "/pay/enable", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String payEnable(@RequestBody @Valid FundSetDTO fundSetDTO, BindingResult br) {
        XbbResponse<FundSetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundSetVO fundSetVO = fundSetService.payEnable(fundSetDTO);
                response = new XbbResponse<>(fundSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
